package com.createidea.scrumfriend.service.statistics;

import java.sql.Date;
import java.util.List;

import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;

import com.createidea.scrumfriend.dao.statistics.StatisticsDao;
import com.createidea.scrumfriend.dao.story.StoryDao;
import com.createidea.scrumfriend.dao.task.TaskDao;
import com.createidea.scrumfriend.to.ProjectTo;
import com.createidea.scrumfriend.to.SprintTo;
import com.createidea.scrumfriend.to.StatisticsDateTo;
import com.createidea.scrumfriend.to.StatisticsProjectTo;
import com.createidea.scrumfriend.to.StatisticsSprintTo;

public class StatisticsServiceImpl implements StatisticsService {
    private StatisticsDao statisticsDao;
    private StoryDao storyDao;
    private TaskDao taskDao;
    
	@Override
	public void createStatisticsForProject(SprintTo sprint) {
		// TODO Auto-generated method stub
		statisticsDao.saveStatistics(setUpStatisticsProject(sprint));
	}
	
	public StatisticsProjectTo setUpStatisticsProject(SprintTo sprint){
		ProjectTo project=sprint.getProject();
		float totalStoryPoint=storyDao.calculateTotalPointsForProject(project);
		float completedStoryPoint=storyDao.calculateCompletedPointForProject(project);
		float remainingStoryPoint=storyDao.calculateRemainingPointForProject(project);
		StatisticsProjectTo statistics=new StatisticsProjectTo();
		statistics.setDate(sprint.getEndTime());
		statistics.setProject(sprint.getProject());
		statistics.setSprintSerial(sprint.getSerial());
		statistics.setCompletedStoryPoint(completedStoryPoint);
		statistics.setRemainingStoryPoint(remainingStoryPoint);
		statistics.setTotalStoryPoint(totalStoryPoint);
		return statistics;
	}
	
    
    public  float[][] createDataForProjectBurnDown(String projectId) {     
	    List<StatisticsProjectTo> statistics=statisticsDao.getProjectStatistics(projectId);	    
	    if(statistics!=null&&statistics.size()>0){
	    String[] colKeys = new String[statistics.size()];
	    float[][] data = new float[2][statistics.size()];
	    for(int i=0; i<statistics.size();i++){
	    	data[0][i]=statistics.get(i).getTotalStoryPoint();
	    	data[1][i]=statistics.get(i).getRemainingStoryPoint();	    	
	    }
	    return data;  
	    }
	    else
	    return null;
    }  
    
	public StatisticsDao getStatisticsDao() {
		return statisticsDao;
	}
	public void setStatisticsDao(StatisticsDao statisticsDao) {
		this.statisticsDao = statisticsDao;
	}

	@Override
	public List<StatisticsProjectTo> getProjectStatistics(String projectId) {
		// TODO Auto-generated method stub
		return statisticsDao.getProjectStatistics(projectId);
	}

	public StoryDao getStoryDao() {
		return storyDao;
	}

	public void setStoryDao(StoryDao storyDao) {
		this.storyDao = storyDao;
	}

	@Override
	public float calculateCommittedStoryPoint(SprintTo sprint) {
		// TODO Auto-generated method stub
		return storyDao.calculateCommittedStoryPoint(sprint.getId());
	}

	@Override
	public float calculateCompletedStoryPoint(SprintTo sprint) {
		// TODO Auto-generated method stub
		return storyDao.calculateCompletedStoryPoint(sprint.getId());
	}

	@Override
	public float calculateTotalEffortForSprint(SprintTo sprint) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public float calculateRemainingEffortForSprint(SprintTo sprint) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void updateStatisticsForSprint(SprintTo sprint) {
		// TODO Auto-generated method stub
		StatisticsSprintTo statistics=sprint.getStatistics();
		if(statistics==null){
			statistics=new StatisticsSprintTo();
			statistics.setSprint(sprint);
		}
		statistics.setCommittedStoryPoint(calculateCommittedStoryPoint(sprint));
		statistics.setCompletedStoryPoint(calculateCompletedStoryPoint(sprint));
		statistics.setTotalEffort(calculateTotalEffortForSprint(sprint));
		statistics.setRemainingEffort(calculateRemainingEffortForSprint(sprint));
		statistics.setRemainingStoryPoint(statistics.getCommittedStoryPoint()-statistics.getCompletedStoryPoint());
		statisticsDao.updateSprintStatistics(statistics);
	}

	@Override
	public CategoryDataset createDataForSprints(String projectId) {
		// TODO Auto-generated method stub
		List<StatisticsSprintTo> statistics=statisticsDao.getSprintsStatisticsForProject(projectId);
		if(statistics!=null&&statistics.size()>0){
			 String[] rowKeys={ "Committed Points", "Completed Points" };
			    String[] colKeys = new String[statistics.size()];
			    double[][] data = new double[2][statistics.size()];
			    for(int i=0; i<statistics.size();i++){
			    	
			    	colKeys[i]=i+"";//+dateFormat.format(statistics.get(i).getDate());
			    	data[0][i]=statistics.get(i).getCommittedStoryPoint();
			    	data[1][i]=statistics.get(i).getCompletedStoryPoint();
			    	
			    }
			    return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data);  	
		}
		return null;
	}

	@Override
	public void setStatisticsDate(SprintTo sprint, Date date) {
		// TODO Auto-generated method stub
		
		StatisticsDateTo statisticsDate=statisticsDao.getStatisticsDateByDate(date);
		if(statisticsDate==null){
			statisticsDate=new StatisticsDateTo();
			statisticsDate.setSprint(sprint);
			statisticsDate.setDate(date);
		}
		statisticsDate.setRemainingEffort(calculateRemainingEffort(sprint));
		statisticsDao.saveOrUpdateStatisticsDate(statisticsDate);
		
	}
	
	public float calculateRemainingEffort(SprintTo sprint){
		return taskDao.calculateRemainingEffortForSprint(sprint);
	}

	public TaskDao getTaskDao() {
		return taskDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	@Override
	public CategoryDataset getDataForSprintBurnDown(String sprintId) {
		// TODO Auto-generated method stub
		List<StatisticsDateTo> statistics=statisticsDao.getStatisticsDateForSprint(sprintId);
		if(statistics!=null&&statistics.size()>0){
			 String[] rowKeys={ "Committed Points", "Completed Points" };
			    String[] colKeys = new String[statistics.size()];
			    double[][] data = new double[1][statistics.size()];
			    for(int i=0; i<statistics.size();i++){		    	
			    	colKeys[i]=i+"";//+dateFormat.format(statistics.get(i).getDate());
			    	data[0][i]=statistics.get(i).getRemainingEffort();	
			    }
			    return DatasetUtilities.createCategoryDataset(rowKeys, colKeys, data);  	
		}
		return null;
	}
	
	
}
